package cn.com.pxto.dao;

import java.util.HashMap;
import java.util.List;

import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.School;

public class SchoolDAO extends HibernatePageDaoSupport {
	public School getSchool(Integer sid){
		return (School) getTemplate().get(School.class, sid);
	}
	
	public void saveSchool(School school){
		getTemplate().save(school);
	}
	
	public void deleteSchool(Integer sid){
		String HQL="from School as s where s.sid="+sid;
		getTemplate().delete(HQL);
	}
	
	public void updateSchool(School school){
		getTemplate().saveOrUpdate(school);
	}
	/*
	 * return null if cant find
	 */
	@SuppressWarnings("unchecked")
	public School getSchool(String name){
		String HQL="select s from School as s where s.name='"+name+"'";
		List list=getTemplate().find(HQL);
		if(list!=null && list.size()>0){
			return (School)list.get(0);
		}else{
			return null;
		}
	}
	public List<School> listSchool(PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select s from School as s  ");
		HQL.append(" order by s.sid");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	public List<School> listSchool(){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select s from School as s  ");
		HQL.append(" order by s.sid");
		return getTemplate().find(HQL.toString());
	}
	@SuppressWarnings("unchecked")
	public HashMap<String ,String> listSchoolForTag(){
		HashMap<String ,String> userMap=new HashMap();
		StringBuffer HQL=new StringBuffer();
		HQL.append("select s from School as s ");
		HQL.append(" order by s.sid");
		List<School> schoolList=getTemplate().find(HQL.toString());
		if (schoolList!=null && schoolList.size()>0) {
			for (int i = 0; i < schoolList.size(); i++) {
				School school=schoolList.get(i);
				userMap.put(school.getSid().toString(), school.getName());
			}
		}
		return userMap;
	}
	/*
	 * 根据名称模糊查询
	 */
	public List<Integer> getSchoolByName(String name){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select s.sid from School as s where s.name like '%");
		HQL.append(name+"%'");
		HQL.append(" order by s.sid");
		return getTemplate().find(HQL.toString());
	}
	
	/*
	 * 根据课程类别模糊查询
	 */
	public List<School> getSchoolByClassType(String typeId){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select s from School as s where s.lessonTypeStr like '%,");
		HQL.append(typeId+",%'");
		HQL.append(" order by s.sid");
		return getTemplate().find(HQL.toString());
	}
}
